﻿/*************** 国标协议转换过程,各种字节转换int，int转字节等 ****************/


/*************** 字节数组转换到国标通信字符串********/
function transToGBHexString(byteArray) {
    var s = new String();
    var i;
    for (i = 0; i < byteArray.length; i++) {
        if (s.length == 0)
            s += parseInt(byteArray[i], 16).toString();
        else
            s += ","+parseInt(byteArray[i], 16).toString();
        
   }
   return s;

}

/**********************国标通信字符串转ASCII**************/
function transGBHexToString(gbhex) {
    var bytes = gbhex.split(',');
    var i;
    var r = "";
    for (i = 0; i < bytes.length; i++) {
        r += String.fromCharCode(parseInt(bytes[i], 16));
    }
    return r;
}



/**********十进制字符串转 16进制字符串**********/
function intStringToHexString(value) {
    return parseInt(value).toString(16);
}

/**********二进制字符串转 16进制字符串**********/
function binStringToHexString(binString) {
    return parseInt(binString, 2).toString(16);
}

function binStringToDWORDString(binString) {
    var a = parseInt(binString, 2);
    return UInt32ToDWORDString(a);
}

function binStringToWORDString(binString) {
    var a = parseInt(binString, 2);
    return UInt16ToWORDString(a);
}



/*********二进制位设置************************
* strBin -- 原二进制数
* bitIndex -- 需设定位， 从右向左
* bitValue -- '0' 或者 '1'
**********************************************/
function bitSet(strBin, bitIndex, bitValue) {
    return replacechr(strBin, strBin.length - bitIndex - 1, bitValue);
}

/***************int值转4字节DWORD 字符串 小端对齐 无逗号间隔*******************/
function UInt32ToDWORDStringNoComm(intvalue) {
    var strHex = parseInt(intvalue).toString(16);
    strHex = padLeft(strHex, '0', 8);
    var i;
    var r = "";
    for (i = 0; i < strHex.length / 2; i++) {
        r += strHex.substr((strHex.length / 2 - 1 - i) * 2, 2);
    }
    return r;
   
}


/***************int值转4字节DWORD 字符串 大端对齐 无逗号间隔*******************/
function UInt32ToBigEndDWORDStringNoComm(intvalue) {
    var strHex = parseInt(intvalue).toString(16);
    strHex = padLeft(strHex, '0', 8);
    var i;
    var r = "";
    for (i = 0; i < strHex.length / 2; i++) {
        r += strHex.substr(i * 2, 2);
    }
    return r;
}

/***************int值转4字节DWORD 字符串 小端对齐*******************/
function UInt32ToDWORDString(intvalue) {
    var strHex = parseInt(intvalue).toString(16);
    strHex = padLeft(strHex, '0', 8);
    var i;
    var r = "";
    for (i = 0; i < strHex.length / 2; i++) {
        if (r.length == 0)
            r += strHex.substr(i * 2, 2);
        else
            r += "," + strHex.substr(i * 2, 2);
    }
    return r;
}

/***************int值转4字节DWORD 字符串 大端对齐*******************/
function UInt32ToBigEndDWORDString(intvalue) {
    var strHex = parseInt(intvalue).toString(16);
    strHex = padLeft(strHex, '0', 8);
    var i;
    var r = "";
    for (i = 0; i < strHex.length / 2; i++) {
        if (r.length == 0)
            r += strHex.substr( (strHex.length / 2 -1- i) * 2, 2);
        else
            r += "," + strHex.substr((strHex.length / 2 - 1 - i) * 2, 2);
    }
    return r;
}

/****************国标16进制字串转数字***************/
function GBStringToUInt32(strGBHex) {
    var strHex = strGBHex.split(',');
    var i;
    var value =0;
    for (i = 0; i < strHex.length; i++) {
        value += Math.pow(256, i) * parseInt(strHex,16);
    }
    return value;
}

function UInt8ToByteString(intvalue) {
   return padLeft(intStringToHexString(intvalue),'0',2);
}

/***************int值转2字节WORD 字符串 小端对齐 无逗号间隔*******************/
function UInt16ToWORDStringNoComm(intvalue) {
    var strHex = parseInt(intvalue).toString(16);
    strHex = padLeft(strHex, '0', 4);
    var i;
    var r = "";
    for (i = 0; i < strHex.length / 2; i++) {
        r += strHex.substr(i * 2, 2);
    }
    return r;
}

/***************int值转2字节WORD 字符串 大端对齐 无逗号间隔*******************/
function UInt16ToBigEndWORDStringNoComm(intvalue) {
    var strHex = parseInt(intvalue).toString(16);
    strHex = padLeft(strHex, '0', 4);
    var i;
    var r = "";
    for (i = 0; i < strHex.length / 2; i++) {
        r += strHex.substr((strHex.length / 2-1-i) * 2, 2);
    }
    return r;
}

/**************int 值转2字节WORD字符串 小端对齐 ********************/
function UInt16ToWORDString(intvalue) {
    var strHex = parseInt(intvalue).toString(16);
    strHex = padLeft(strHex, '0', 4);
    var i;
    var r = "";
    for (i = 0; i < strHex.length / 2; i++) {
        if (r.length == 0)
            r += strHex.substr(i * 2, 2);
        else
            r += "," + strHex.substr(i * 2, 2);
    }
    return r;
}

function WORDStringToUint16(strGBHex) {
}

/**************date 值转6字节BCD码*******************/
function DateToBCDString(date) {
    var myDate = new Date(date);
    var year = String(myDate.getFullYear()).substr(2, 2);
    var month = padLeft(String((myDate.getMonth() + 1)), "0", 2);
    var day = padLeft(String(myDate.getDate()), "0", 2);
    var hours = padLeft(String(myDate.getHours()), "0", 2);
    var minutes = padLeft(String(myDate.getMinutes()), "0", 2);
    var seconds = padLeft(String(myDate.getSeconds()), "0", 2);
    var result = year + "," + month + "," + day + "," +  hours + "," + minutes + "," + seconds;
    return result;
}

/***************字符串转ASC 16进制字符串*****************/
function StringToASCHex(txtstring) {
    var s="";
    for (var i = 0; i < txtstring.length; i++) {
        s += padLeft(txtstring.charCodeAt(i).toString(16),'0',2);
    }
    return s;
}

/*****************16进制ASC字符串转字符****************/
function ASCHexToString(aschexstr) {
    var s ="";
    for (var i = 0; i < aschexstr.length / 2; i++) {
        var ascvalue = parseInt(aschexstr.substr(2 * i, 2), 16);
        s += String.fromCharCode(ascvalue).toString();

    }
    return s;
}

